package com.cbd.project.tms.gateway.handler;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

//@Component
public class SentinelBlockRequestHandler implements BlockRequestHandler {
    @Override
    public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("code", 429);
        bodyMap.put("msg", "请求过于频繁，请稍后重试");
        bodyMap.put("route", serverWebExchange.getRequest().getPath().value());
        bodyMap.put("timestamp", System.currentTimeMillis());
        return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(bodyMap));
    }
}
